import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:pass_log/app/core/utils/adapt.dart';
import 'package:pass_log/app/data/services/theme_service.dart';
import 'package:pass_log/app/modules/home/widgets/home_pass_list.dart';

typedef ObjItem = Map<String, dynamic>;

class HomeMainListController extends GetxController {
  var position = (-1).obs;
}

class HomeMainList extends StatelessWidget {
  final List<ObjItem> mainList;
  const HomeMainList({super.key, required this.mainList});

  static final ctr = Get.put(HomeMainListController());

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: Adapt.pt(375),
      child: Obx(
        () => ExpansionPanelList(
          children: mainList.map((item) => _buildItem(item)).toList(),
          animationDuration: const Duration(milliseconds: 200),
          expansionCallback: (index, open) {
            ctr.position.value = open ? index : -1;
          },
        ),
      ),
    );
  }

  ExpansionPanel _buildItem(ObjItem item) {
    return ExpansionPanel(
      isExpanded: mainList.indexOf(item) == ctr.position.value,
      canTapOnHeader: true,
      // 外层密码分类
      headerBuilder: (context, index) => Flex(
        direction: Axis.horizontal,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(width: Adapt.pt(20)),
          Container(
            height: Adapt.pt(36),
            width: Adapt.pt(36),
            decoration: BoxDecoration(
              color: ThemeService.to.curColorScheme.primaryContainer,
              shape: BoxShape.circle,
            ),
            child: Icon(IconData(int.parse(item["icon"]), fontFamily: item["fontFamily"])),
          ),
          SizedBox(width: Adapt.pt(20)),
          Container(
            alignment: Alignment.center,
            height: Adapt.pt(50),
            child: Text(
              item["name"],
              style: TextStyle(
                color: ThemeService.to.curColorScheme.onPrimaryContainer,
                fontSize: Adapt.pt(18),
              ),
            ),
          ),
        ],
      ),
      // 内层密码列表
      body: Container(
        decoration: BoxDecoration(color: ThemeService.to.curColorScheme.secondaryContainer),
        child: HomePassList(passList: item["children"]),
      ),
    );
  }
}
